package com.yang.inventorymanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.inventorymanagement.mapper.ProductsMapper;
import com.yang.inventorymanagement.pojo.Inventory;
import com.yang.inventorymanagement.pojo.Products;
import com.yang.inventorymanagement.pojo.ProductsDto;
import com.yang.inventorymanagement.pojo.Suppliers;
import com.yang.inventorymanagement.service.InventoryService;
import com.yang.inventorymanagement.service.ProductsService;
import com.yang.inventorymanagement.service.SuppliersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【products】的数据库操作Service实现
 * @createDate 2023-12-22 09:35:53
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products>
        implements ProductsService {

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private SuppliersService suppliersService;

    @Override
    public List<Products> selectProductsByProductName(String productName) {
        return null;
    }


    /**
     * 查找商品库存的相关消息
     *
     * @return
     */
    @Override
    public Object[][] getAllproducts() {


        QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("IsDeleted", 1);
        List<Products> productsList = baseMapper.selectList(queryWrapper);


        if (productsList != null) {
            Object[][] productsListMSG = new Object[productsList.size()][3];
            for (int i = 0; i < productsList.size(); i++) {
                productsListMSG[i][0] = productsList.get(i).getProductid();
                productsListMSG[i][1] = productsList.get(i).getProductname();
                productsListMSG[i][2] = productsList.get(i).getQuantity();

            }
            return productsListMSG;

        }


        return new Object[0][0];


    }

    /**
     * 根据商品id查询和供应商相关的库存等信息
     *
     * @param productsIds
     * @return
     */
    @Override
    public Object[][] getAllProductsAndSuppliers(Integer productsIds) {
        if (productsIds == null) {
            return null;
        }
        List<ProductsDto> productsDtoList = new ArrayList<>();
        //商品信息
        Products products = baseMapper.selectById(productsIds);
        Suppliers suppliersServiceById;
        List<Inventory> allListInventory1 = inventoryService.getAllListInventory(productsIds);
        for (int i = 0; i < allListInventory1.size(); i++) {
            Integer supplierid = allListInventory1.get(i).getSupplierid();
            suppliersServiceById = suppliersService.getById(supplierid);
            ProductsDto productsDto = new ProductsDto();
            productsDto.setProductname(products.getProductname());
            productsDto.setProductid(products.getProductid());
            productsDto.setQuantity(products.getQuantity());
            productsDto.setSuppliername(suppliersServiceById.getSuppliername());
            productsDto.setQuantitySuppliers(allListInventory1.get(i).getQuantity());
            productsDto.setSupplierid(supplierid);
            productsDtoList.add(productsDto);
        }
        if (productsDtoList != null) {
            Object[][] productsListMSG = new Object[productsDtoList.size()][6];
            for (int i = 0; i < productsDtoList.size(); i++) {
                productsListMSG[i][0] = productsDtoList.get(i).getProductid();
                productsListMSG[i][1] = productsDtoList.get(i).getProductname();
                productsListMSG[i][2] = productsDtoList.get(i).getQuantity();
                productsListMSG[i][3] = productsDtoList.get(i).getSupplierid();
                productsListMSG[i][4] = productsDtoList.get(i).getSuppliername();
                productsListMSG[i][5] = productsDtoList.get(i).getQuantitySuppliers();

            }
            return productsListMSG;
        }
        return new Object[0][];
    }

    /**
     * 根据商品名称
     *
     * @param name
     * @return
     */
    @Override
    public Object[][] getSearchProducts(String name) {
        if (name == null) {
            return new Object[0][0];
        }
        QueryWrapper<Products> suppliersQueryWrapper = new QueryWrapper<>();
        suppliersQueryWrapper.like("ProductName", name);
        List<Products> productsList = baseMapper.selectList(suppliersQueryWrapper);


        if (productsList != null) {
            Object[][] productsListMSG = new Object[productsList.size()][3];
            for (int i = 0; i < productsList.size(); i++) {
                productsListMSG[i][0] = productsList.get(i).getProductid();
                productsListMSG[i][1] = productsList.get(i).getProductname();
                productsListMSG[i][2] = productsList.get(i).getQuantity();


            }
            return productsListMSG;
        }
        return new Object[0][0];
    }

    /**
     * 新增商品
     *
     * @param products
     * @return
     */
    @Override
    public int insertProducts(Products products) {
        if (products == null) {
            return 0;
        }
        int insert = baseMapper.insert(products);
        return insert;
    }
}




